﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.MISInterface;
using System.Data;
using System.Reflection;
using MISAPP.Classify.DataAccess;


namespace MISAPP.Classify.Service
{
    public class Utility
    {
        public List<string> Login(string p_id, string p_pw,string p_ip)
        {
            //MISAPP.Classify.DataAccess.Utility utility = new MISAPP.Classify.DataAccess.Utility();
            DataAccess.DA_UserInfo usr = new MISAPP.Classify.DataAccess.DA_UserInfo();
            System.Data.DataTable dt = usr.GetUserInfo(p_id, p_pw);

            List<string> result = new List<string>();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Rows[0][i].ToString().Length > 0)
                    {
                        result.Add(dt.Rows[0][i].ToString());
                    }
                }
                usr.SetLoginInfo(p_id, p_ip);
            }
            return result;
        }
        public static bool GetExclusiveOfAccount(string p_exclusive)
        {
            return p_exclusive.Equals("1") ? true : false;
        }

        public DataTable GetUserSequared(int p_level,string p_userGroup)
        {
            DataAccess.SEQUARED_LEVEL userLevel = (DataAccess.SEQUARED_LEVEL)p_level;
            DataAccess.DA_UserInfo usr = new MISAPP.Classify.DataAccess.DA_UserInfo();
            return usr.GetSequared(userLevel,p_userGroup);
        }

        public static void ReleaseCOMObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                throw new Exception("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        public static bool ChangeUserPW(string p_id, string p_oldPW, string p_newPW)
        {
            DataAccess.DA_UserInfo usr = new MISAPP.Classify.DataAccess.DA_UserInfo();
            int result = usr.ChangeUserPW(p_id, p_oldPW, p_newPW);
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 檢查是否為日期
        /// </summary>
        /// <param name="p_ym">201104</param>
        /// <returns></returns>
        public static bool IsDate(string p_ym)
        {
            bool ok = true;
            string dte = string.Empty;
            try
            {
                dte = string.Format("{0}-{1}-01", p_ym.Substring(0, 4)
               , p_ym.Substring(4));
                Convert.ToDateTime(dte);                
            }
            catch (Exception ex)
            {
                ok = false;
            }
            return ok;
        }

        public static string GetYYYYMM(DateTime p_d)
        {
            string y, m;
            y = p_d.Year.ToString();
            m = string.Format("0{0}", p_d.Month.ToString());
            m = m.Length > 2 ? m.Substring(1) : m;
            return y + m;
        }

        public static string GetNewRange(string p_col, string p_row)
        {
            char[] chs = p_col.ToCharArray();
            string rng = Convert.ToChar(Convert.ToInt16(chs[0])
                + DateTime.Now.AddMonths(-1).Month).ToString();

            return string.Format("{0}{1}", rng, p_row);
        }

        /// <summary>
        /// 依日期區間產生出所有月份資料
        /// </summary>
        /// <param name="p_months"></param>
        /// <returns></returns>
        public static string[] GetAllMonths(string[] p_months)
        {
            DateTime d1, d2;
            d1 = Convert.ToDateTime(string.Format("{0}-{1}-01", p_months[0].Substring(0, 4)
                , p_months[0].Substring(4)));
            d2 = Convert.ToDateTime(string.Format("{0}-{1}-01", p_months[1].Substring(0, 4)
                            , p_months[1].Substring(4)));

            int cnt = d2.Month - d1.Month + ((d2.Year - d1.Year) * 12) + 1;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cnt; i++)
            {
                sb.AppendFormat("{0},", Utility.GetYYYYMM(d1.AddMonths(i)));
            }
            return sb.ToString().Split(new char[] { ',' }
                , StringSplitOptions.RemoveEmptyEntries);
        }


        public static MethodInfo FindMethodInfo(MethodInfo[] p_mi
            , string p_methodName , Type[] p_paraTypes)
        {
            bool ok = true;
            System.Reflection.MethodInfo rtnMethod = null;
            foreach (System.Reflection.MethodInfo m in p_mi)
            {
                if (m.Name.ToLower().Equals(p_methodName.ToLower()))
                {
                    ok = true;
                    System.Reflection.ParameterInfo[] paraInfo = m.GetParameters();
                    if (p_paraTypes == null)
                    {
                        rtnMethod = m;
                        break;
                    }
                    //if (paraInfo.Length != p_paraTypes.Length){ break; }
                    if (paraInfo.Length == p_paraTypes.Length)
                    {
                        for (int i = 0; i < paraInfo.Length; i++)
                        {
                            //確認型別都是我要的
                            if (paraInfo[i].ParameterType != p_paraTypes[i])
                            {
                                ok = false;
                                break;
                            }
                        }
                        rtnMethod = (ok == true) ? m : null;
                        if (rtnMethod != null) { break; }
                    }

                }
            }
            return rtnMethod;
        }

        /// <summary>
        /// 取得自MISParameter 中，Distinct 後的fldname
        /// </summary>
        /// <param name="p_team">MISParameter.team</param>
        /// <returns></returns>
        public static string[] GetSheets(string p_team)
        {
            DA_MISParameter para = new DA_MISParameter();
            System.Collections.ArrayList al = para.GetDistinctFldname(p_team);
            return (string[])al.ToArray(typeof(string));
        }

        /// <summary>
        /// 把字串做加解密
        /// </summary>
        /// <param name="p_value"></param>
        /// <returns></returns>
        public static string GetEncryptOrDeEncrypt(string p_value)
        {
            int key = 100;
            StringBuilder inSb = new StringBuilder(p_value);
            StringBuilder outSb = new StringBuilder(p_value.Length);
            char c;
            for (int i = 0; i < p_value.Length; i++)
            {
                c = inSb[i];
                c = (char)(c ^ key);
                outSb.Append(c);
            }
            return outSb.ToString();
        }

        /// <summary>
        /// 取得所輸入年月中的最後一天
        /// </summary>
        /// <param name="p_ym"></param>
        /// <returns></returns>
        public static DateTime GetLastDate(string p_ym)
        {
            DateTime d1 = DateTime.MinValue;
            string ym = "{0}-{1}-1";
            try
            {
                if (Utility.IsDate(p_ym))
                {
                    d1 = Convert.ToDateTime(string.Format(
                        ym, p_ym.Substring(0, 4), p_ym.Substring(4, 2)))
                        .AddMonths(1).AddDays(-1);
                }                
            }
            catch (Exception ex)
            { 
            }
            return d1;
        }
    }



}
